package com.ycb.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ycb.common.Result;
import com.ycb.pojo.Employee;
import com.ycb.service.IEmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 员工信息 前端控制器
 * </p>
 *
 * @author ycb
 * @since 2024-09-30
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    /**
     * 员工登录接口
     * @param httpServletRequest 获取请求头
     * @param employee 获取登录信息
     * @return
     * @throws Exception
     */
    @PostMapping("/login")
    public Result<Employee> login(HttpServletRequest httpServletRequest, @RequestBody Employee employee) throws Exception {
        return employeeService.login(httpServletRequest,employee);
    }

    /**
     * 员工退出功能
     * @param httpServletRequest
     * @return
     * @throws Exception
     */
    @PostMapping("/logout")
    public Result<String> logout(HttpServletRequest httpServletRequest) throws Exception {
        //清除request中的userInfo信息
        httpServletRequest.getSession().removeAttribute("employee");
        return Result.success("退出成功");
    }

    /**
     * 新增员工
     *
     * @param employee
     * @return
     */
    @PostMapping
    public Result<String> save(HttpServletRequest request, @RequestBody Employee employee) {
        log.info("新增的员工信息：{}", employee.toString());
        //设置默认密码为123456，并采用MD5加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //存入数据库
        employeeService.save(employee);
        return Result.success("添加员工成功");
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     * @throws Exception
     */
    @GetMapping("/page")
    public Result<Page<Employee>> page(int page, int pageSize, String name) throws Exception {
        log.info("page={},pageSize={},name={}", page, pageSize, name);
        //构造分页构造器
        Page<Employee> pageInfo = new Page<>(page, pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        //添加过滤条件（当我们没有输入name时，就相当于查询所有了）
        wrapper.like(!(name == null || "".equals(name)), Employee::getName, name);
        //并对查询的结果进行降序排序，根据更新时间
        wrapper.orderByDesc(Employee::getUpdateTime);
        //执行查询
        employeeService.page(pageInfo, wrapper);
        return Result.success(pageInfo);
    }

    @GetMapping("/{id}")
    public Result<Employee> getEmployeeById(@PathVariable String id) throws Exception {
        if (id == null) {
            return Result.error("传入的id为空");
        }
        Employee employeeServiceById = employeeService.getById(id);
        if (employeeServiceById == null) {
            log.info("查询id错误：{}",id);
            return Result.error("查询数据错误");
        }

        return Result.success(employeeServiceById);
    }

    /**
     * 通用的修改员工信息
     *
     * @param employee
     * @param request
     * @return
     */
    @PutMapping
    public Result<String> update(@RequestBody Employee employee, HttpServletRequest request) {
        log.info(employee.toString());
        //获取线程id
        long id = Thread.currentThread().getId();
        log.info("update的线程id为：{}", id);
        employeeService.updateById(employee);
        return Result.success("员工信息修改成功");
    }
}
